package com.ruoyi.record.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.RecordAttendMapper;
import com.ruoyi.record.domain.RecordAttend;
import com.ruoyi.record.service.IRecordAttendService;
import com.ruoyi.common.core.text.Convert;

/**
 * 考勤Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Service
public class RecordAttendServiceImpl extends ServiceImpl<RecordAttendMapper, RecordAttend> implements IRecordAttendService
{
    /**
     * 查询考勤
     * 
     * @param attendId 考勤主键
     * @return 考勤
     */
    @Override
    public RecordAttend selectRecordAttendByAttendId(Long attendId)
    {
        return baseMapper.selectRecordAttendByAttendId(attendId);
    }

    /**
     * 查询考勤列表
     * 
     * @param recordAttend 考勤
     * @return 考勤
     */
    @Override
    public List<RecordAttend> selectRecordAttendList(RecordAttend recordAttend)
    {
        return baseMapper.selectRecordAttendList(recordAttend);
    }

    /**
     * 新增考勤
     * 
     * @param recordAttend 考勤
     * @return 结果
     */
    @Override
    public int insertRecordAttend(RecordAttend recordAttend)
    {
        recordAttend.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(recordAttend);
    }

    /**
     * 修改考勤
     * 
     * @param recordAttend 考勤
     * @return 结果
     */
    @Override
    public int updateRecordAttend(RecordAttend recordAttend)
    {
        recordAttend.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recordAttend);
    }

    /**
     * 批量删除考勤
     * 
     * @param attendIds 需要删除的考勤主键
     * @return 结果
     */
    @Override
    public int deleteRecordAttendByAttendIds(String attendIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(attendIds)));
    }

    /**
     * 删除考勤信息
     * 
     * @param attendId 考勤主键
     * @return 结果
     */
    @Override
    public int deleteRecordAttendByAttendId(Long attendId)
    {
        return baseMapper.deleteById(attendId);
    }

    /**
     * 校验考勤
     *
     * @param recordAttend 考勤
     * @return 结果
     */
    @Override
    public boolean checkRecordAttend(RecordAttend recordAttend) {
        Long attendId = StringUtils.isNull(recordAttend.getAttendId()) ? -1L : recordAttend.getAttendId();
        RecordAttend info = baseMapper.selectOne(new LambdaQueryWrapper<RecordAttend>()
                .eq(RecordAttend::getAttendMonth, recordAttend.getAttendMonth())
                .eq(RecordAttend::getRemark, recordAttend.getRemark())
                .eq(RecordAttend::getDeptId, recordAttend.getDeptId())
                .last("limit 1"));
        if (StringUtils.isNotNull(info) && info.getAttendId().longValue() != attendId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 提交考勤信息
     *
     * @param attendIds 考勤主键
     * @return 结果
     */
    @Override
    public int submitAttend(String attendIds)
    {
        return baseMapper.update(null, new LambdaUpdateWrapper<RecordAttend>()
                .set(RecordAttend::getAttendStatus, "1").in(RecordAttend::getAttendId, Convert.toLongArray(attendIds)));
    }

    /**
     * 撤回考勤信息
     *
     * @param attendIds 考勤主键
     * @return 结果
     */
    @Override
    public int recallAttend(String attendIds)
    {
        return baseMapper.update(null, new LambdaUpdateWrapper<RecordAttend>()
                .set(RecordAttend::getAttendStatus, "0").in(RecordAttend::getAttendId, Convert.toLongArray(attendIds)));
    }
}
